package com.jic.order.impl;

import com.alibaba.fastjson.JSONObject;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.order.entity.OrderGoods;
import com.jic.order.mapper.OrderGoodsMapper;
import com.jic.order.response.HotProductResponse;
import com.jic.order.response.OrderGoodsResponse;
import com.jic.order.service.OrderGoodsService;
import com.jic.product.after.api.HotProductApi;
import com.jic.product.after.api.ProductListApi;
import com.jic.product.store.request.HotProductRequest;
import com.jic.product.after.response.ProductAfterInfoByIdResponse;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 订单商品明细表
 *
 * @author : yinhaijun
 * @version : v1.0.0
 * @email :
 * @since : 2020-01-13 08:52:20
 */
@Service
public class OrderGoodsServiceImpl implements OrderGoodsService {

	@Resource
	private OrderGoodsMapper orderGoodsMapper;

	@Resource
	private ProductListApi productListApi;
	
	@Resource
    private HotProductApi hotProductApi;



    @Override
	public List<OrderGoods> query(OrderGoods orderGoods) {
		return orderGoodsMapper.query(orderGoods);
	}

	@Override
	public PageResult<OrderGoods> queryPage(OrderGoods orderGoods, Page pageQuery) {
		// 计算下标
		int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
		List<OrderGoods> list = orderGoodsMapper.queryPage(startIndex, pageQuery.getLimit(), orderGoods);
		if (CollectionUtils.isNotEmpty(list)) {
			list.stream()
					.filter(item -> item.getGoodsId() != 0)
					.forEach(item -> {
						RestResult<ProductAfterInfoByIdResponse> productAfterInfoByIdResponse = productListApi.queryProductInfoById(item.getGoodsId());
						if (null != productAfterInfoByIdResponse) {
							item.setPropertyName(productAfterInfoByIdResponse.getData().getPropertyName());
							item.setProductCode(productAfterInfoByIdResponse.getData().getCode());
							item.setArtNo(productAfterInfoByIdResponse.getData().getArtNo());
							item.setBarCode(productAfterInfoByIdResponse.getData().getBarCode());
							item.setUnitName(productAfterInfoByIdResponse.getData().getUnitName());
						}
					});
		}
		long count = orderGoodsMapper.queryPageCount(orderGoods);
		PageResult pageResult = new PageResult();
		pageResult.setRows(list);
		pageResult.setTotal(count);
		return pageResult;
	}

	@Override
	public RestResult<Integer> queryProductSalesByProductId(List<Long> productIds) {
		return RestResult.success(orderGoodsMapper.queryProductSalesByProductId(productIds));
	}

	@Override
	public RestResult<List<HotProductResponse>> queryHotProduct() {
		return RestResult.success(orderGoodsMapper.queryHotProduct());
	}

	@Override
	public OrderGoods getGoodsInfoByOrderNo(String orderNo) {
		return orderGoodsMapper.getGoodsInfoByOrderNo(orderNo);
	}

    @Override
    public List<OrderGoodsResponse> queryTopTenGoodes() {
        return orderGoodsMapper.queryTopTenGoodes();
    }

    @Override
    public RestResult<Long> insertProductbyOrder(OrderGoodsResponse queryTopTenGoodes) {
        HotProductRequest hotProductRequest = new HotProductRequest();
        BeanUtils.copyProperties(queryTopTenGoodes, hotProductRequest);
        hotProductRequest.setProductId(queryTopTenGoodes.getGoodsId());
        RestResult<Long> longRestResult = hotProductApi.saveHotProduct(hotProductRequest);
        return longRestResult;
    }

	@Override
	public Integer updateWzCardNo(JSONObject jsonObject) {
		return orderGoodsMapper.updateWzCardNo(jsonObject);
	}

	@Override
	public List<JSONObject> queryCardGoods() {
		return orderGoodsMapper.queryCardGoods();
	}

	@Override
	public List<JSONObject> querySettOrder(JSONObject jsonObject) {
		return orderGoodsMapper.querySettOrder(jsonObject);
	}

	@Override
	public List<JSONObject> querySettOrderByPay(JSONObject jsonObject) {
		return orderGoodsMapper.querySettOrderByPay(jsonObject);
	}

	@Override
	public List<OrderGoodsResponse> queryByOrderNoAndGoodsCode(String commodityCode, String orderNo) {
		return orderGoodsMapper.queryByOrderNoAndGoodsCode(commodityCode,orderNo);
	}
}